﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Linq;
using Pavilion.GameTools;

namespace Pavilion.Locks
{
    class SpeedYLock : ILock
    {
        public float SpeedY { get; set; }
        public Operand Operand { get; set; }

        public void Deserialize(XElement lockElement, string resourceName)
        {
            SpeedY = float.Parse(lockElement.Attribute("SpeedY").Value);
            Operand = (Operand)Enum.Parse(typeof(Operand), lockElement.Attribute("Operand").Value);
        }

        public XElement Serialize()
        {
            XElement lockElement = new XElement("Lock");

            lockElement.Add(new XAttribute("SpeedY", SpeedY));
            lockElement.Add(new XAttribute("Operand", Operand));

            return lockElement;
        }

        public LockMonitorBase CreateLockMonitor(GameItem gameItem)
        {
            PolygonPhysicalData polygonData = gameItem.ObtainTool<PhysicalTypeTool>().PolygonData;

            if (Operand == Operand.GreaterThen)
                return new LockPredicateMonitor(polygonData, () => polygonData.SpeedY < SpeedY);
            else if (Operand == Operand.LessThen)
                return new LockPredicateMonitor(polygonData, () => polygonData.SpeedY > SpeedY);
            else
                return new LockPredicateMonitor(polygonData, () => polygonData.SpeedY == SpeedY);
        }

        public override string ToString()
        {
            return this.GetType().Name;
        }
        }
    }

